import React, { Component } from 'react';
import { Form, Select, Input, Modal } from 'antd';
import axios from 'axios';
import { connect } from 'react-redux';
import Tips from '../../Tips/index';
import statuscode from '../../statuscode/index';
const FormItem = Form.Item;
const Option = Select.Option;
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
class joinChaincode extends Component {
  constructor() {
    super();
    this.state = {
      myorgid: []
    };
  }
  componentWillUnmount() {
    this.ismounted = false;
  }
  componentWillMount() {
    this.ismounted = true;
    var $this = this;
    const Token = localStorage.getItem('token');
    setTimeout(() => {
    });
  }
  inputchange=(ev) => {
    if (!ev.target.value) return;
    this.setState({
      message: 'loading'
    });
    const $this = this;
    const Token = localStorage.getItem('token');
    var TransactionData = {};
    axios({
      method: 'get',
      url: 'api/block/chainCode/queryChainCodeById',
      headers: {
        'Content-Type': 'application/json',
        Authorization: Token
      },
      params: { channcodeId: ev.target.value }
    })
      .then(function (response) {
        if ($this.ismounter) {
          var code = response.data.code;
          if (statuscode[response.data.code] == undefined) code = 500;
          var message = statuscode[code];
          if (code == 200 && response.data.data) {
            $this.props.form.setFieldsValue({
              name: response.data.data.channelcodename,
              version: response.data.data.channelversion,
              network: response.data.data.fabricflag
            });
          } else if (code == 200 && !response.data.data) {
            message = '无效的邀请码！';
          }
          $this.setState({
            message: message,
          }, () => {
            setTimeout(() => {
              $this.setState({
                message: ''
              });
            }, 1000);
          });
        }
      })
      .catch(function (error) {
        console.log(error);
        if ($this.ismounted) {
          $this.setState({
            message: '请求失败，请刷新重试'
          }, () => {
            setTimeout(() => {
              $this.setState({
                message: ''
              });
            }, 1000);
          });
        }
      });
  }
  render() {
    const { visible, onCancel, onCreate, form } = this.props;
    const { getFieldDecorator } = form;
    var myorgidList = '';
    if (this.state.myorgid.length !== 0) {
      myorgidList = this.state.myorgid.map((e, i) => {
        return (<Option key={i} value={''}>{''}</Option>);
      });
    }
    return (
      <div>
        <Modal
          visible={visible}
          title="安装已有Chaincode"
          okText="安装"
          cancelText="取消"
          onCancel={onCancel}
          closable={false}
          onOk={onCreate.bind(this, this)}
        >
          <Form>
            <FormItem
              label="邀请码"
              labelCol={{ span: 6 }}
              wrapperCol={{ span: 16 }}
            >
              {getFieldDecorator('invitation', {
                rules: [{ required: true, message: '请输入Chaincode安装邀请码!' }],
              })(
                <Input placeholder="请输入Chaincode安装邀请码!" onPressEnter={this.inputchange} onBlur={this.inputchange} />
              )}
            </FormItem>
            <FormItem
              label="网络"
              labelCol={{ span: 6 }}
              wrapperCol={{ span: 16 }}
            >
              {getFieldDecorator('network', {
                rules: [{ required: true, message: '请先输入邀请码!' }],
              })(
                <Input rows={4} disabled placeholder="请先输入邀请码" />
              )}
            </FormItem>
            <FormItem
              label="Name"
              labelCol={{ span: 6 }}
              wrapperCol={{ span: 16 }}
            >
              {getFieldDecorator('name', {
                rules: [{ required: true, message: '请先输入邀请码!', }],
              })(
                <Input rows={4} disabled placeholder="请先输入邀请码" />
              )}
            </FormItem>
            <FormItem
              label="Version"
              labelCol={{ span: 6 }}
              wrapperCol={{ span: 16 }}
            >
              {getFieldDecorator('version', {
                rules: [{ required: true, message: '请先输入邀请码!' }],
              })(
                <Input rows={4} disabled placeholder="请先输入邀请码" />
              )}
            </FormItem>
          </Form>
        </Modal>
        <Tips message={this.state.message} />
      </div>
    );
  }
}
const JoinChaincode = Form.create()(joinChaincode);
// export default JoinChaincode;
export default connect((state) => {
  return { ...state };
})(JoinChaincode);
